package com.candy.service.impl;

import com.candy.bean.News;
import com.candy.dao.NewsMapper;
import com.candy.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    NewsMapper newsMapper;

    /**
     * 查询未删除状态下的用户信息
     * @param news
     * @return
     */
    @Override
    public List<News> getAllNews(News news) {
        List<News> newsInfos=newsMapper.getAllNews(news);
        return newsInfos;
    }

    /**
     * 新增或修改操作；根据主键id是否为空判断是新增还是编辑
     * @param news
     * @return
     */
    @Override
    public Integer addOrUpdateNews(@RequestBody News news) {
        Integer id=news.getId();
        Integer num;
        if(id==null||id.equals("")){
            num=newsMapper.insertSelective(news);
        }else{
            num=newsMapper.updateByPrimaryKeySelective(news);
        }
        return num;
    }

    /**
     * 删除操作：采用逻辑删除实现此目的。通过改变isdeleted的值来修改用户状态
     * @param news
     * @return
     */
    @Override
    public Integer deleteNews(News news) {
        news.setIsdeleted(1);
        Integer num=newsMapper.updateByPrimaryKeySelective(news);
        return num;
    }
}
